package com.wxl.common.util;

import android.graphics.Typeface;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;

import java.lang.reflect.Field;

/**
 * Copyright：贵州玄机科技有限公司
 * Created by wxl on 2020/8/26 13:55
 * Description：
 * Modify time：
 * Modified by：
 */
public class TabHelper {

    public static void onTabSelectedChangedTextSize(TabLayout.Tab tab, float size,int width, boolean isSelected){
        TextView textView = findTextViewByTab(tab.view);
        ViewGroup.LayoutParams params = tab.view.getLayoutParams();
        if (textView != null) {
            if (isSelected) {
                textView.setScaleX(1.2f);
                textView.setScaleY(1.2f);
                textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            } else {
                textView.setScaleX(1.0f);
                textView.setScaleY(1.0f);
                textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            }
            textView.setTextSize(size);
        }
        params.width = DPUtil.dip2px(width);
        tab.parent.setTabGravity(TabLayout.GRAVITY_START);
    }


    private static TextView findTextViewByTab(TabLayout.TabView tabView) {
        try {
            Field field = TabLayout.TabView.class.getDeclaredField("textView");
            field.setAccessible(true);
            TextView textView = (TextView) field.get(tabView);
            return textView;
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }


}
